﻿using System.Linq;
using DKE_Kinect.Entwicklung.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DKE_Kinect.Entwicklung.Grafik
{
    /// <summary>
    /// Klase für einen Cursor im 3D Raum
    /// </summary>
    public class Cursor3D
    {
        /// <summary>
        /// Position des Cursors
        /// </summary>
        private Vector3 pPosition;
        /// <summary>
        /// Vorherige Position des Cursors
        /// </summary>
        private Vector3 pPrevPosition;
        /// <summary>
        /// Model das den Cursor repräsentiert
        /// </summary>
        private Model pModel;

        /// <summary>
        /// Gets the position.
        /// </summary>
        public Vector3 Position
        { get { return pPosition; } }
        /// <summary>
        /// Gets the old position.
        /// </summary>
        public Vector3 PrevPosition
        { get { return pPrevPosition; } }
        /// <summary>
        /// Kosntruktor. Erstellt einen Cursor an der angegebenen Position
        /// </summary>
        /// <param name="_pos">StartPosition für den Cursor</param>
        public Cursor3D(Vector3 _pos)
        {
     
            this.pPosition = _pos;
            this.pPrevPosition = Position;
            this.pModel = ContentManager.loadContent<Model>("objects/SphereLowPoly");
        }

        /// <summary>
        /// Zeichnet den Cursor
        /// </summary>
        /// <param name="_gameTime">Spielzeit</param>
        /// <param name="_camera">Aktuelle Kamera</param>
        public void Draw(GameTime _gameTime,Camera3D _camera)
        {
            Matrix[] transforms = new Matrix[pModel.Bones.Count];
            pModel.CopyAbsoluteBoneTransformsTo(transforms);

            // Draw the model. A model can have multiple meshes, so loop.
            ModelMesh[] allMeshes = pModel.Meshes.ToArray();
            foreach (ModelMesh mesh in allMeshes)
            {
                // This is where the mesh orientation is set, as well 
                // as our camera and projection.
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] 
                        * Matrix.CreateTranslation(pPosition);
                    effect.View = _camera.ViewMatrix;
                    effect.Projection = _camera.ProjectionMatrix;
                }
                // Draw the mesh, using the effects set above.
                mesh.Draw();
             
            }

        }
        /// <summary>
        /// Bewegt den Cursor
        /// </summary>
        /// <param name="_move">Vektor um den bewegt werden soll</param>
        public void Move(Vector3 _move)
        {
            this.pPrevPosition = new Vector3(pPosition.X, pPosition.Y, pPosition.Z);
            this.pPosition = Vector3.Add(pPosition, _move);
        }
        /// <summary>
        /// Setzt cursor auf angegebene Postition
        /// </summary>
        /// <param name="_pos">Zu setzende Position</param>
        public void SetTo(Vector3 _pos)
        {
            this.pPrevPosition = new Vector3(pPosition.X, pPosition.Y, pPosition.Z);
            this.pPosition =  _pos;
        }
    }
}
